<template>
    <div class="MyPermission-container">

        <el-row>
            <el-col :span="8">
                <el-tree
                        :data="data"
                        :props="defaultProps"
                        :default-expand-all="true"
                        :check-strictly="false"
                        node-key="id"
                        v-loading="loading"
                        highlight-current
                        show-checkbox
                        @node-click="handleNodeClick"
                        ref="tree"
                ></el-tree>
            </el-col>

            <el-col :span="16"></el-col>
        </el-row>
    </div>
</template>

<script>
    import {mapGetters} from 'vuex'
    import { fetchTree } from '@/api/upms/permission'
    export default {
        name: 'MyPermission',
        data() {
            return {
                loading: false,
                data: [],
                idKeys: [],
                defaultProps: {
                    label: 'name',
                    children: 'children'
                },
            }
        },
        created() {
            this.getDataTree()
        },
        computed: {
            ...mapGetters(['permissions'])
        },
        methods: {
            back() {
                this.$router.back()
            },
            handleNodeClick(data) {
                this.$refs.tree.setChecked(data.id, true)
            },
            getDataTree() {
                this.loading = true
                fetchTree().then(res => {
                    this.data = res.data
                    this.checkPermissions()
                    this.loading = false
                }).catch(error => {
                    this.loading = false
                    console.log(error)
                })
            },
            checkPermissions() {
                setTimeout(()=>{
                    for(let item of this.permissions) {
                        this.$refs.tree.setChecked(item.id, true, true)
                    }
                }, 500)

            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>